<!--
 * @Descripttion: 文件说明
 * @version: 0.0.1
 * @Author: zhangmeng
 * @Date: 2022-08-21 12:08:39
 * @LastEditors: zhangmeng
 * @LastEditTime: 2022-08-22 08:26:27
-->
<template>
  <div class="design-panel">
    <div class="top-panel panel">
      <div class="header">
        <div class="header-active" :style="{ left: offset }" />
        <div class="header-nav">
          <div
            class="nav-label"
            v-for="(item, index) in nav"
            :key="index"
            @click="handle(item)"
          >
            <span>{{ item.label }}</span>
          </div>
        </div>
      </div>
      <div class="panel-complex">
        <div
          class="panel-collapse"
          v-for="(item, index) in complexList"
          :key="index"
        >
          <div class="panel-collapse-title" @click="handleCollapse(item)">
            <span>{{ item.title }}</span>
            <svg
              v-if="item.show && item.complex.length > 0"
              t="1661753597377"
              class="icon"
              viewBox="0 0 1024 1024"
              p-id="2935"
              width="14"
              height="14"
            >
              <path
                d="M512 714.666667c-8.533333 0-17.066667-2.133333-23.466667-8.533334l-341.333333-341.333333c-12.8-12.8-12.8-32 0-44.8 12.8-12.8 32-12.8 44.8 0l320 317.866667 317.866667-320c12.8-12.8 32-12.8 44.8 0 12.8 12.8 12.8 32 0 44.8L533.333333 704c-4.266667 8.533333-12.8 10.666667-21.333333 10.666667z"
                p-id="2936"
                fill="#bfbfbf"
              ></path>
            </svg>
            <svg
              v-else
              t="1661753559186"
              class="icon"
              viewBox="0 0 1024 1024"
              p-id="2613"
              width="14"
              height="14"
            >
              <path
                d="M320 885.333333c-8.533333 0-17.066667-4.266667-23.466667-10.666666-12.8-12.8-10.666667-34.133333 2.133334-44.8L654.933333 512 298.666667 194.133333c-12.8-10.666667-14.933333-32-2.133334-44.8 10.666667-12.8 32-14.933333 44.8-2.133333l384 341.333333c6.4 6.4 10.666667 14.933333 10.666667 23.466667 0 8.533333-4.266667 17.066667-10.666667 23.466667l-384 341.333333c-6.4 6.4-12.8 8.533333-21.333333 8.533333z"
                p-id="2614"
                fill="#bfbfbf"
              ></path>
            </svg>
          </div>
          <div
            class="panpel-collapse-content"
            v-if="item.show && item.complex.length > 0"
            :style="{
              borderBottom:
                index == complexList.length - 1
                  ? 'none'
                  : '1px solid rgb(219, 219, 219)',
            }"
          >
            <div
              class="component-item"
              v-for="(item, index) in item.complex"
              :key="index"
              draggable
              @dragstart="(e) => handleDragStart(e, item)"
            >
              <svg-icon class="component-icon" :icon="item.name" />
              <div class="component-item-title">{{ item.title }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="foot-panel panel"
      :style="{
        height: panelShow ? '320px' : '36px',
      }"
    >
      <div class="panel-title foot-panel-title" @click="openPanel">
        <span>图层</span>
        <span
          class="icon"
          :style="{
            transform: `rotate(${panelShow ? -90 : 0}deg)`,
          }"
        >
          <svg
            t="1661753597377"
            class="icon"
            viewBox="0 0 1024 1024"
            p-id="2935"
            width="14"
            height="14"
          >
            <path
              d="M512 714.666667c-8.533333 0-17.066667-2.133333-23.466667-8.533334l-341.333333-341.333333c-12.8-12.8-12.8-32 0-44.8 12.8-12.8 32-12.8 44.8 0l320 317.866667 317.866667-320c12.8-12.8 32-12.8 44.8 0 12.8 12.8 12.8 32 0 44.8L533.333333 704c-4.266667 8.533333-12.8 10.666667-21.333333 10.666667z"
              p-id="2936"
              fill="#bfbfbf"
            ></path>
          </svg>
        </span>
      </div>
      <div class="panel-layer">
        <div
          class="layer-item"
          :class="{ active: item.focused }"
          v-for="(item, index) in list"
          :key="index"
        >
          <div class="layer-item-left">
            <svg-icon class="layer-icon" :icon="item.name" />
            <span class="layer-label">{{ item.config.label }}</span>
          </div>
          <div class="layer-item-right">
            <div class="layer-item-right-btn">
              <svg-icon class="svg-icon" v-if="item.lock" icon="lock" />
              <svg-icon class="svg-icon" v-else icon="unlock" />
            </div>
            <div class="layer-item-right-btn">
              <svg-icon class="svg-icon" v-if="item.hidden" icon="eye-off" />
              <svg-icon class="svg-icon" v-else icon="eye-on" />
            </div>
            <div class="layer-item-right-btn">
              <svg-icon class="svg-icon" icon="editable" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ComplexList from "@/library/components";
export default {
  props: {
    list: Array,
  },
  data() {
    return {
      key: 0,
      panelShow: false,
      nav: [
        {
          key: 0,
          label: "组件库",
        },
        {
          key: 1,
          label: "我的组件",
        },
        {
          key: 2,
          label: "收藏",
        },
      ],
      complexList: ComplexList(),
    };
  },
  computed: {
    offset() {
      return `${this.key * 69 + 16}px`;
    },
  },
  methods: {
    handle(item) {
      this.key = item.key;
    },
    openPanel() {
      this.panelShow = !this.panelShow;
    },
    // 展开组件库
    handleCollapse(item) {
      item.show = !item.show;
    },
    // 拖拽元素
    handleDragStart(e, item) {
      const data = {
        item,
        offset: {
          x: e.offsetX,
          y: e.offsetX,
        },
      };
      // 存储拖拽元素
      this.$store.dispatch("design/setDrag", data);
    },
  },
};
</script>

<style lang="scss" scoped>
$fontColor: rgb(102, 102, 102);

.panel-complex {
  width: 100%;
  height: calc(100% - 36px);
  overflow-y: auto;
  overflow-x: hidden;

  &::-webkit-scrollbar {
    display: none;
  }
}

.panel-collapse {
  width: 100%;

  .panel-collapse-title {
    width: 100%;
    height: 36px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 16px;
    box-sizing: border-box;
    padding-right: 4px;
    border-bottom: 1px solid rgb(219, 219, 219);
    line-height: 36px;
    color: $fontColor;
    font-size: 12px;
  }

  .panpel-collapse-content {
    width: 100%;
    border-bottom: 1px solid rgb(219, 219, 219);
    padding: 4px;
    box-sizing: border-box;
    transition: all 0.58s ease;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    grid-template-rows: 70px;

    .component-item {
      width: 100%;
      height: 70px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      background: rgb(247, 247, 247);
      color: $fontColor;
      border-radius: 4px;
      cursor: pointer;

      &:hover {
        background: #ddd;
      }

      &:active {
        cursor: grabbing;
      }
    }

    .component-icon {
      font-size: 24px;
      color: $fontColor;
    }

    .component-item-title {
      margin: 6px auto;
      width: 100%;
      font-size: 12px;
      text-align: center;
      color: $fontColor;
    }
  }
}

.design-panel {
  position: relative;
  width: 100%;
  height: 100%;

  .panel {
    width: 100%;

    .header {
      position: relative;
      width: 100%;
      height: 36px;
      padding: 0 16px;
      box-sizing: border-box;
      border-bottom: 1px solid rgb(219, 219, 219);

      .header-active {
        position: absolute;
        left: 207px;
        bottom: 0px;
        width: 30%;
        border-bottom: 2px solid #409eff;
        transition: all 0.28s ease;
      }

      .header-nav {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-start;

        .nav-label {
          flex: 1;
          color: #000;
          text-align: center;
          font-size: 14px;
          cursor: pointer;
        }
      }
    }

    .panel-title {
      padding-left: 16px;
      padding-right: 6px;
      border-top: 1px solid rgb(219, 219, 219);
      border-bottom: 1px solid rgb(219, 219, 219);
      height: 36px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      color: $fontColor;
      font-weight: bold;
      font-size: 12px;
    }
  }

  .top-panel {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  .foot-panel {
    position: absolute;
    bottom: 0px;
    width: 100%;
    height: 320px;
    transition: all 0.58s ease;
    background: #fff;

    .foot-panel-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .icon {
      display: flex;
      justify-content: space-between;
      align-items: center;
      transform: rotate(0deg);
    }

    .panel-layer {
      width: 100%;
      height: calc(100% - 36px);
      overflow-y: auto;
      overflow-x: hidden;

      .layer-item {
        position: relative;
        width: 100%;
        height: 32px;
        padding-left: 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 12px;
        cursor: pointer;

        .layer-item-right {
          position: absolute;
          right: -86px;
          width: 86px;
          display: flex;
          justify-content: flex-start;
          align-items: center;

          .svg-icon {
            width: 14px;
            margin: 0 4px;
          }
        }

        .layer-label {
          flex: 1;
          text-align: left;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .layer-icon {
          font-size: 14px;
          margin-right: 6px;
        }

        &:hover {
          background: rgb(218, 235, 254);

          .layer-item-right {
            right: 0;
          }
        }

        &.active {
          background: rgb(218, 235, 254);
        }
      }
    }
  }
}
</style>
